package com.jeecg.p3.system.poster.web;

import com.jeecg.p3.base.util.ComCode;
import com.jeecg.p3.base.util.DateUtil;
import com.jeecg.p3.system.advisor.entity.StudyAdviser;
import com.jeecg.p3.system.advisor.service.StudyAdvService;
import com.jeecg.p3.system.base.BaseController;
import com.jeecg.p3.system.poster.entity.WhitePerson;
import com.jeecg.p3.system.poster.service.WhitePersonService;
import com.jeecg.p3.system.util.HtmlUtil;
import com.jeecg.p3.system.vo.LoginUser;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/system/back/poster/whiteperson")
public class WhitePersonController extends BaseController {
    @Autowired
    private WhitePersonService service;//白名单处理层
    @Autowired
    private StudyAdvService studyAdvService;

    /**
     *
     * @param query 白名单查询对象
     * @param request
     * @param response
     * @param pageNo 第几页
     * @param pageSize 页数大小
     * @throws Exception
     */
    @RequestMapping(value="/list.do",method = {RequestMethod.GET,RequestMethod.POST})
    public void toIndividual(@ModelAttribute WhitePerson query, HttpServletRequest request, HttpServletResponse response,
                             @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
                             @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception{
        PageQuery<WhitePerson> pageQuery = new PageQuery<WhitePerson>();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        VelocityContext velocityContext = new VelocityContext();
        pageQuery.setQuery(query);
        velocityContext.put("whiteperson",query);
        velocityContext.put("pageInfos", SystemTools.convertPaginatedList(service.queryPageList(pageQuery)));
        String viewName = "system/back/poster/white_person.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 跳转到新增白名单界面页面
     * @return
     */
    @RequestMapping(value = "/toAdd",method ={RequestMethod.GET, RequestMethod.POST})
    public void toAddDialog(HttpServletRequest request, HttpServletResponse response, ModelMap model)throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        String viewName = "system/back/poster/white_person_add.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 保存白名单信息
     * @return
     */
    @RequestMapping(value = "/doAdd",method ={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxJson doAdd(@ModelAttribute WhitePerson poster, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser loginUser = getCurrentUser(request);
            poster.setCreateDate(DateUtil.dateToString(new Date()));
            poster.setCreateTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            poster.setCreateUser(loginUser.getId());
            poster.setStatus(ComCode.statusNormal);
            service.doAdd(poster);
            j.setMsg("保存成功");
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("保存失败");
        }
        return j;
    }

    @RequestMapping(value="/anth",method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson anth(@RequestParam(required = true, value = "id" ) Long id, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            WhitePerson whitePerson = new WhitePerson();
            LoginUser loginUser = getCurrentUser(request);
            whitePerson.setCreateDate(DateUtil.dateToString(new Date()));
            whitePerson.setCreateTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            whitePerson.setCreateUser(loginUser.getId());
            whitePerson.setStatus(ComCode.statusNormal);
            StudyAdviser adviser = this.studyAdvService.findById(id);
            if(StringUtils.isEmpty(adviser.getOpenId())) {
                j.setSuccess(false);
                j.setMsg("请先激活顾问在进行设置");
                return j;
            }
            WhitePerson param = new WhitePerson();
            param.setOpenId(adviser.getOpenId());
            List<WhitePerson> list = service.findByParam(param);
            if(list != null && list.size() > 0) {
                j.setSuccess(false);
                j.setMsg("该顾问已被设置成白名单，请勿重新设置！");
                return j;
            }
            whitePerson.setOpenId(adviser.getOpenId());
            whitePerson.setNickName(adviser.getNickName());
            whitePerson.setPhoto(adviser.getPhoto());
            service.doAdd(whitePerson);
            j.setMsg("设置成功！");
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("设置异常:"+e.getMessage());
            e.printStackTrace();
        }
        return j;
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value="/doDelete",method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson doDelete(@RequestParam(required = true, value = "id" ) Long id, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser user = getCurrentUser(request);
            WhitePerson whitePerson = service.findById(id);
            whitePerson.setDeleteDate(DateUtil.dateToString(new Date()));
            whitePerson.setDeleteTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            whitePerson.setDeleteUser(user.getId());
            service.doDelete(whitePerson);
            j.setMsg("删除成功");
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("删除失败:"+e.getMessage());
        }
        return j;
    }
}
